// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2023, Phytium Technology Co., Ltd.
 * lixinde			<lixinde@phytium.com.cn>
 * weichangzheng	<weichangzheng@phytium.com.cn>
 * This file describes the initialization of sec of
 * the Phytium pe2201 board.
 */


#include <common.h>
#include <stdio.h>
#include <string.h>
#include <linux/arm-smccc.h>
#include "board.h"
#include "../cpu.h"
#include "../parameter/parameter.h"

struct common_config {
	u32 magic;
	u32 version;
	u32 size;
	u8 rev1[4];
	u64 core_bit_map;
} __attribute((aligned(4)));

struct common_config const common_base_info = {
	.magic = PARAMETER_COMMON_MAGIC,
	.version = 0x1,
	.core_bit_map = 0x3333,
};

/*
 * use smc instruction to realize sec init
 */
void sec_init(uint8_t s3_flag)
{
	u8 buffer[0x100];
	struct arm_smccc_res res;
	uint32_t s3_fun = 0;

	/* get parameter from parameter table */
	if ((get_parameter_info(PM_COMMON, buffer, sizeof(buffer)))) {
		printf("use default parameter\n");
		memcpy(buffer, &common_base_info, sizeof(common_base_info));
	}
	if (s3_flag)
		s3_fun = 1;
	arm_smccc_smc(CPU_INIT_SEC_SVC, s3_fun, (u64)buffer, 0, 0, 0, 0, 0, &res);
	if (res.a0 != 0) {
		printf("error ret  %lx\n", res.a0);
		while (1);
	}
}
